package com.cskaoyan.service;

import com.cskaoyan.bean.Permission;
import com.cskaoyan.bean.PermissionExample;
import com.cskaoyan.bean.SystemPermission;
import com.cskaoyan.bean.SystemPermissionExample;
import com.cskaoyan.bean.sysmanage.SystemPermissionsVO;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.mapper.SystemPermissionMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SystemPermissionServiceImpl implements SystemPermissionService {

    @Autowired
    SystemPermissionMapper systemPermissionMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public boolean insertSystemPermission(SystemPermissionsVO systemPermissionsVO) {
        List<SystemPermissionsVO.SystemPermissionsDTO> systemPermissions = systemPermissionsVO.getSystemPermissions();
        //第一层数据 SystemPermissionsDTO
        for (SystemPermissionsVO.SystemPermissionsDTO systemPermission : systemPermissions) {
            SystemPermission insertSystemPermission = new SystemPermission();
            insertSystemPermission.setPermissionId(systemPermission.getId());
            insertSystemPermission.setLabel(systemPermission.getLabel());
            insertSystemPermission.setAddTime(new Date());
            insertSystemPermission.setUpdateTime(new Date());
            int insertRows = systemPermissionMapper.insertSelective(insertSystemPermission);
            if (insertRows == 0) return false;
        }

        int firstCount = systemPermissionMapper.getCount();
        //第二层数据 ChildrenInfo
        for (SystemPermissionsVO.SystemPermissionsDTO systemPermission : systemPermissions) {
            List<SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo> children = systemPermission.getChildren();
            for (SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo child : children) {
                SystemPermission insertSystemPermission = new SystemPermission();
                insertSystemPermission.setPermissionId(child.getId());
                insertSystemPermission.setLabel(child.getLabel());
                //获取父id
                SystemPermissionExample systemPermissionExample = new SystemPermissionExample();
                SystemPermissionExample.Criteria criteria = systemPermissionExample.createCriteria();
                if (!StringUtils.isEmpty(systemPermission.getId())) {
                    criteria.andLabelEqualTo(systemPermission.getId());
                }
                List<SystemPermission> systemPermissions1 = systemPermissionMapper.selectByExample(systemPermissionExample);
                if (systemPermissions1.size() != 0 && systemPermissions1 != null) {
                    insertSystemPermission.setPid(systemPermissions1.get(0).getId());
                }
                insertSystemPermission.setAddTime(new Date());
                insertSystemPermission.setUpdateTime(new Date());
                int insertRows = systemPermissionMapper.insertSelective(insertSystemPermission);
                if (insertRows == 0) return false;
            }
        }

        int secondCount = systemPermissionMapper.getCount();
        //第三层数据 GrandSonInfo
        for (SystemPermissionsVO.SystemPermissionsDTO systemPermission : systemPermissions) {
            List<SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo> children = systemPermission.getChildren();
            for (SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo child : children) {
                List<SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo.GrandSonInfo> grandSons = child.getChildren();
                for (SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo.GrandSonInfo grandSon : grandSons) {
                    SystemPermission insertSystemPermission = new SystemPermission();
                    insertSystemPermission.setPermissionId(grandSon.getId());
                    insertSystemPermission.setLabel(grandSon.getLabel());
                    insertSystemPermission.setApi(grandSon.getApi());
                    insertSystemPermission.setAddTime(new Date());
                    insertSystemPermission.setUpdateTime(new Date());
                    //获取父id
                    SystemPermissionExample systemPermissionExample = new SystemPermissionExample();
                    SystemPermissionExample.Criteria criteria = systemPermissionExample.createCriteria();
                    if (!StringUtils.isEmpty(child.getId())) {
                        criteria.andLabelEqualTo(child.getId());
                    }
                    List<SystemPermission> systemPermissions1 = systemPermissionMapper.selectByExample(systemPermissionExample);


                    if ("admin:notice:list".equals(insertSystemPermission.getPermissionId())) {
                        insertSystemPermission.setPid(systemPermissions1.get(systemPermissions1.size()-1).getId());
                    } else {
                        insertSystemPermission.setPid(systemPermissions1.get(0).getId());

                    }

                    int insertRows = systemPermissionMapper.insertSelective(insertSystemPermission);
                    if (insertRows == 0) return false;
                }
            }
        }
        return true;
    }

    @Override
    public SystemPermissionsVO queryByRoleId(Integer roleId) {
        SystemPermissionExample systemPermissionExample = new SystemPermissionExample();
        SystemPermissionExample.Criteria systemPermissionExampleCriteria = systemPermissionExample.createCriteria();
        //pid = 0 查询出所有父菜单
        systemPermissionExampleCriteria.andPidEqualTo(0);
        List<SystemPermission> systemPermissions = systemPermissionMapper.selectByExample(systemPermissionExample);
        SystemPermissionsVO systemPermissionsVO = new SystemPermissionsVO();

        ArrayList<SystemPermissionsVO.SystemPermissionsDTO> systemPermissionsDTOList = new ArrayList<>();

        for (SystemPermission systemPermission : systemPermissions) {
            SystemPermissionsVO.SystemPermissionsDTO systemPermissionsDTO = new SystemPermissionsVO.SystemPermissionsDTO();

            systemPermissionsDTO.setId(systemPermission.getPermissionId());
            systemPermissionsDTO.setLabel(systemPermission.getLabel());

            Integer primaryKey = systemPermission.getId();
            SystemPermissionExample systemPermissionExample2 = new SystemPermissionExample();
            SystemPermissionExample.Criteria systemPermissionExampleCriteria2 = systemPermissionExample2.createCriteria();
            systemPermissionExampleCriteria2.andPidEqualTo(primaryKey);
            List<SystemPermission> children = systemPermissionMapper.selectByExample(systemPermissionExample2);

            List<SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo> childrenInfoVos = new ArrayList<>();

            for (SystemPermission childrenInfo : children) {
                SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo childrenInfoVo = new SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo();
                childrenInfoVo.setId(childrenInfo.getPermissionId());
                childrenInfoVo.setLabel(childrenInfo.getLabel());

                ArrayList<SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo.GrandSonInfo> grandSonInfoVos = new ArrayList<>();

                Integer childrenInfoId = childrenInfo.getId();
                SystemPermissionExample systemPermissionExample3 = new SystemPermissionExample();
                SystemPermissionExample.Criteria systemPermissionExampleCriteria3 = systemPermissionExample3.createCriteria();
                systemPermissionExampleCriteria3.andPidEqualTo(childrenInfoId);
                List<SystemPermission> grandsonInfoList = systemPermissionMapper.selectByExample(systemPermissionExample3);
                for (SystemPermission grandSonInfo : grandsonInfoList) {
                    SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo.GrandSonInfo grandSonInfoVo = new SystemPermissionsVO.SystemPermissionsDTO.ChildrenInfo.GrandSonInfo();
                    grandSonInfoVo.setId(grandSonInfo.getPermissionId());
                    grandSonInfoVo.setLabel(grandSonInfo.getLabel());
                    grandSonInfoVo.setApi(grandSonInfo.getApi());
                    grandSonInfoVos.add(grandSonInfoVo);
                }
                childrenInfoVos.add(childrenInfoVo);
                childrenInfoVo.setChildren(grandSonInfoVos);
            }
            systemPermissionsDTO.setChildren(childrenInfoVos);

            systemPermissionsDTOList.add(systemPermissionsDTO);
        }


        systemPermissionsVO.setSystemPermissions(systemPermissionsDTOList);

        //assignedPermissions赋值
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        if (roleId != null) {
            criteria.andRoleIdEqualTo(roleId);
        }
        List<Permission> permissions = permissionMapper.selectByExample(permissionExample);
        ArrayList<String> permissionList = new ArrayList<>();
        for (Permission permission : permissions) {
            String rolePermission = permission.getPermission();
            permissionList.add(rolePermission);
        }
        systemPermissionsVO.setAssignedPermissions(permissionList);

        return systemPermissionsVO;
    }
}
